# install seaborn
!pip install seaborn
Seaborn Part 1
Seaborn is a Python data visualization library based on matplotlib. Seaborn makes it easy to create informative and attractive statistical graphics. We’ll cover basic plotting techniques and explore some of the functionalities Seaborn offers.
Installing Seaborn
Before we begin, make sure you have Seaborn installed. You can install it via pip if you haven’t already:
### Importing Seaborn and Other Libraries
import seaborn as sns
import matplotlib.pyplot as plt
Loading Sample Dataset
Seaborn comes with some built-in datasets for practice. For this tutorial, we’ll use the “tips” dataset, which contains information about tips given in a restaurant.
= sns.load_dataset("tips")
tips ### Basic Plots
tips.head()
total_bill | tip | sex | smoker | day | time | size | |
---|---|---|---|---|---|---|---|
0 | 16.99 | 1.01 | Female | No | Sun | Dinner | 2 |
1 | 10.34 | 1.66 | Male | No | Sun | Dinner | 3 |
2 | 21.01 | 3.50 | Male | No | Sun | Dinner | 3 |
3 | 23.68 | 3.31 | Male | No | Sun | Dinner | 2 |
4 | 24.59 | 3.61 | Female | No | Sun | Dinner | 4 |
Here are the columns typically found in the “tips” dataset:
- total_bill: Total bill amount (including tip).
- tip: Tip amount.
- sex: Gender of the person paying the bill (male or female).
- smoker: Whether the party included smokers (yes or no).
- day: The day of the week.
- time: Whether the meal was lunch or dinner.
- size: The size of the dining party (number of people).
1. Scatter Plot
='total_bill', y='tip', data=tips)
sns.scatterplot(x'Scatter Plot of Total Bill vs Tip')
plt.title( plt.show()
2. Line Plot
='size', y='total_bill', data=tips)
sns.lineplot(x'Line Plot of Party Size vs Total Bill')
plt.title( plt.show()
3. Histogram
'total_bill'], bins=10, kde=True)
sns.histplot(tips['Histogram of Total Bill')
plt.title( plt.show()
4. Bar Plot
='size', y='total_bill', data=tips)
sns.barplot(x'Average Total Bill by Day')
plt.title( plt.show()
Customizing Plots
1. Changing Color Palette
"dark")
sns.set_palette(='day', y='total_bill', data=tips)
sns.barplot(x'Average Total Bill by Day')
plt.title( plt.show()
2. Adding Labels and Legends
='total_bill', y='tip', hue='sex', data=tips)
sns.scatterplot(x'Scatter Plot of Total Bill vs Tip')
plt.title('Total Bill ($)')
plt.xlabel('Tip ($)')
plt.ylabel(='Gender')
plt.legend(title plt.show()
3. Adding Annotations
='total_bill', y='tip', data=tips)
sns.scatterplot(x'Scatter Plot of Total Bill vs Tip')
plt.title('This point seems interesting', xy=(20, 2), xytext=(25, 4),
plt.annotate(=dict(facecolor='black', shrink=0.05))
arrowprops plt.show()
Advanced Visualizations
1. Pair Plot
='sex')
sns.pairplot(tips, hue'plot.png')
plt.savefig( plt.show()
2. Heatmap
# Exclude non-numeric columns from correlation calculation
= tips.select_dtypes(include=['float64', 'int64'])
numeric_cols = numeric_cols.corr()
correlation_matrix
# Plotting the heatmap
=True)
sns.heatmap(correlation_matrix, annot'Correlation Heatmap')
plt.title( plt.show()
3. Violin Plot
='day', y='total_bill', data=tips, split=True)
sns.violinplot(x'Violin Plot of Total Bill by Day and Gender')
plt.title( plt.show()
Conclusion
Seaborn provides a high-level interface for drawing attractive and informative statistical graphics. This tutorial covered basic plotting techniques and some advanced visualizations. Experiment with different plot types and customization options to create visualizations tailored to your specific needs.